package net.takela.common.spring.http;

import net.takela.common.spring.exception.ParamException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpMethod;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zhangyinghui
 * create at 2023/8/29
 */
public class HttpRequestBuilder<T> {
    private HttpRequestTemplate restTemplate;
    private Map<String, String> requestHeaders;
    private Map queryParams;
    private Class<T> resultType;
    private Object requestBody;
    private HttpMethod httpMethod;
    private String url;
    private String baseUrl;
    private String path;
    private Map<String, String> uriVariables;
    /**
     * 
     */
    public HttpRequestBuilder setRestTemplate(HttpRequestTemplate restTemplate) {
        this.restTemplate = restTemplate;
        return this;
    }
    /**
     * 
     */
    public HttpRequestBuilder setRequestHeaders(Map<String, String> requestHeaders){
        this.requestHeaders = requestHeaders;
        return this;
    }
    /**
     * 
     */
    public HttpRequestBuilder setQueryParams(Map<String, Object> queryParams){
        this.queryParams = queryParams;
        return this;
    }
    /**
     * 
     */
    public HttpRequestBuilder setResultType(Class resultType){
        this.resultType = resultType;
        return this;
    }
    /**
     * 
     */
    public HttpRequestBuilder setRequestBody(Object requestBody) {
        this.requestBody = requestBody;
        return this;
    }
    /**
     * 
     */
    public HttpRequestBuilder setHttpMethod(HttpMethod httpMethod){
        this.httpMethod = httpMethod;
        return this;
    }
    /**
     * 
     */
    public HttpRequestBuilder setUrl(String url) {
        this.url = url;
        return this;
    }
    /**
     * 
     */
    public HttpRequestBuilder setBaseUrl(String baseUrl) {
        this.baseUrl = baseUrl;
        return this;
    }
    /**
     * 
     */
    public HttpRequestBuilder setPath(String path) {
        this.path = path;
        return this;
    }

    /**
     * 替换路径中的占位符 例如 /api/v1/module/{id}?param1={val1}
     * @param uriVariables
     */
    /**
     * 
     */
    public void setUriVariables(Map<String, String> uriVariables) {
        this.uriVariables = uriVariables;
    }
    /**
     * 
     */
    public static <T> HttpRequestBuilder<T> builder(){
        return new HttpRequestBuilder<T>();
    };
    /**
     * 
     */
    public HttpRequest<T> build(){
        HttpRequest<T> httpRequest = new HttpRequest  (this.restTemplate);
        Map headers = new HashMap();
        if (this.requestHeaders != null){
            headers.putAll(requestHeaders);
        }
        httpRequest.setRequestHeaders(headers);
        Map finallyParams = new HashMap();
        if (this.queryParams != null) {
            finallyParams.putAll(this.queryParams);
        }
        httpRequest.setQueryParams(this.queryParams);
        httpRequest.setResultType(this.resultType);
        httpRequest.setRequestBody(this.requestBody);
        httpRequest.setHttpMethod(this.httpMethod);
        httpRequest.setUrl(this.url);
        if (StringUtils.isEmpty(httpRequest.getUrl())){
            throw new ParamException("param.error.desc","url");
        }
        return httpRequest;
    }
}
